<template>
    <div>
        <h2>会员列表</h2>
        <div>
            姓名:<input v-model="info.userName">
            手机号:<input v-model="info.tel">
            会员等级:<select v-model="info.levelId">
                    <option value="">请选择</option>
                    <option :value="item.memberId" v-for="item in levelList">{{item.memberName}}</option>
                </select>
                <input type="button" value="查询" class="btn btn-success" @click="GetMemberList">
                <input type="button" value="重置" class="btn btn-success" @click="rest">
        </div>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>序号</td>
                    <td>姓名</td>
                    <td>手机号</td>
                    <td>证件类型</td>
                    <td>证件号码</td>
                    <td>出生日期</td>
                    <td>性别</td>
                    <td>状态</td>
                    <td>金额</td>
                    <td>爱好</td>
                    <td>会员等级</td>
                    <td>操作</td>
                </tr>
                <tr :key="item.id" v-for="item in list">
                    <td>{{item.id}}</td>
                    <td>{{item.userName}}</td>
                    <td>{{item.tel.substring(0,3)+'****'+item.tel.substring(7)}}</td>
                    <td>{{item.cardType}}</td>
                    <td>{{item.card}}</td>
                    <td>{{moment(item.birthday).format('YYYY-MM-DD hh:mm:ss')}}</td>
                    <td>{{item.gender==1?'男':'女'}}</td>
                    <td>{{item.state==false?'开启':'禁用'}}</td>
                    <td>{{item.money}}</td>
                    <td>{{item.likes}}</td>
                    <td>{{item.memberName}}</td>
                    <td><input type="button" value="修改" class="btn btn-success" @click="upd(item.id)" >
                    <input type="button" value="删除" class="btn btn-success" @click="del(item.id)" ></td>
                </tr>
            </tbody>
        </table>
        {{ list }}
    </div>
</template>

<script setup lang="ts">
import router from '@/router';
import axios from 'axios';
import moment from 'moment';
import {ref,reactive,onMounted} from 'vue';
import {useRouter} from 'vue-router'
//声明一个变量向后端传递参数
let info:any=reactive({
    userName:'',
    tel:'',
    levelId:''
})

//重置
const rest=()=>
{
    info.userName='',
    info.tel='',
    info.levelId=''
}
//声明一个变量存储后端返回的数据
let list:any=ref([

]);
onMounted (()=>
{
    GetMemberList();
    getLeavelList();
})
let levelList:any=ref([]);
const getLeavelList=()=>{
    axios({
        url:'/api/Member/GetMemberList',
        method:'get',
    })
    .then((res)=>
    {
        console.log(res);
        levelList.value=res.data;
    })
    .catch((err)=>
    {
        
        console.log(err);
    })

};
const GetMemberList =()=>
{
    axios({
        url:'/api/Member/GetMembersList',
        method:'get',
        params:info
    })
    .then((res)=>
{
    console.log(res);
    list.value=res.data;
})
.catch((err)=>
{
    console.log(err);
})
}
//删除
const del=(id:any)=>
{
    console.log(id);
    //1.友好提示 2.发送请求删除操作 3.更新页面
    var f=confirm("请问您确定要删除吗?");
    if(f)
    {
        axios({
            url:'/api/Member/DelMemberItem',
            method:"delete",
            params:{
                id:id,
            },
        })
            .then(res=>
            {
                console.log(res);
                if(res.data)
            {
                alert('删除成功');
                GetMemberList();
            }
            else
            {
                alert('删除失败');
                return;
            }
            })
             .catch((err)=>
            {
                console.log(err);
            })
    }
}
//修改
const upd=(id:any)=>
{
    router.push({
        path:'/memberupdate',
        query:{
            id:id
        }
    });
}
</script>

<style scoped>

</style>